package com.zaaach.citypicker.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 城市数据管理类
 */
public class CityManager {

    private List<HotCityBean> hotCityList;

    private List<LocatedCityBean> localCityList;

    private static CityManager sInstance;

    private CityManager() {
        hotCityList = new ArrayList<>();
        localCityList = new ArrayList<>();
    }

    public static CityManager getInstance() {
        if (sInstance == null) {
            synchronized (CityManager.class) {
                if (sInstance == null) {
                    sInstance = new CityManager();
                }
            }
        }
        return sInstance;
    }

    /**
     * 初始化数据
     */
    public void initData(Context context) {
        try {
            AddressJson baseData = new Gson().fromJson(new JsonReader(new InputStreamReader(context.getAssets().open("city1.json"))),
                    AddressJson.class);
            hotCityList = baseData.getHot();
            localCityList = baseData.getData();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<HotCityBean> getHotCityList() {
        return hotCityList;
    }

    public List<LocatedCityBean> getLocalCityList() {
        return localCityList;
    }


    /**
     * 获取定位城市的基本信息
     */
    public City getLocalCity(String cityName) {

        City city = null;
        for (LocatedCityBean bean : getLocalCityList()) {
            for (AreaBean data : bean.getSubItem()) {
                if (data.getName().equals(cityName)) {
                    String name = data.getName();
                    String province = data.getParentId() + "";
                    String pinyin = data.getQ();
                    String code = data.getId() + "";
                    city = new City(name, province, pinyin, code);
                    break;
                }
            }
        }
        return city;
    }

    /**
     * 获取所有城市
     */
    public List<City> getAllCities() {
        List<City> listData = new ArrayList<>();
        for (LocatedCityBean bean : getLocalCityList()) {
            for (AreaBean data : bean.getSubItem()) {
                String name = data.getName();
                String province = data.getParentId() + "";
                String pinyin = data.getQ();
                String code = data.getId() + "";
                City city = new City(name, province, pinyin, code);
                listData.add(city);
            }
        }
        CityComparator comparator = new CityComparator();
        Collections.sort(listData, comparator);
        return listData;
    }

    /**
     * 获取热门城市
     */
    public List<HotCity> getHotCities() {
        List<HotCity> listData = new ArrayList<>();
        for (HotCityBean data : getHotCityList()) {
            String name = data.getName();
            String province = data.getParentId() + "";
            String code = data.getId() + "";
            HotCity city = new HotCity(name, province, code);
            listData.add(city);
        }
        return listData;
    }

    public List<City> searchCity(final String keyword) {
        List<City> listData = new ArrayList<>();
        for (LocatedCityBean bean : getLocalCityList()) {
            for (AreaBean data : bean.getSubItem()) {
                if (data.getName().startsWith(keyword)) {
                    String name = data.getName();
                    String province = data.getParentId() + "";
                    String pinyin = data.getQ();
                    String code = data.getId() + "";
                    City city = new City(name, province, pinyin, code);
                    listData.add(city);
                }
            }
        }
        CityComparator comparator = new CityComparator();
        Collections.sort(listData, comparator);
        return listData;
    }

    /**
     * 抓取指定城市的区县
     */
    public ArrayList<City> getZone(City data) {
        List<City> listData = new ArrayList<>();
        listData.add(data);
        for (LocatedCityBean bean : localCityList) {
            if (bean.getId() == Integer.parseInt(data.getProvince())) {
                for (AreaBean area : bean.getSubItem()) {
                    if (area.getId() == Integer.parseInt(data.getCode())) {
                        for (AreaBean2 area2 : area.getSubItem()) {
                            String name = area2.getName();
                            String province = area2.getParentId() + "";
                            String pinyin = area2.getQ();
                            String code = area2.getId() + "";
                            City city = new City(name, province, pinyin, code);
                            listData.add(city);
                        }
                    }
                }
            }
        }
        return (ArrayList<City>) listData;
    }

    /**
     * sort by a-z
     */
    private class CityComparator implements Comparator<City> {
        @Override
        public int compare(City lhs, City rhs) {
            String a = lhs.getPinyin().substring(0, 1);
            String b = rhs.getPinyin().substring(0, 1);
            return a.compareTo(b);
        }
    }
}
